#!/usr/bin/env python
import pygtk
pygtk.require("2.0")
import gtk, gobject
from share import Share
from window_template import WindowTemplate

###
protocols = ["samba","ssh","nfs","ftp","web"]
locations = ["location1","location2","location3"]
###
        
class ShareWindow(WindowTemplate):
    def __init__(self,share=None):
        """constructor"""
        self.frames = {"samba":self.create_smb_frame(),
                       "ssh":self.create_ssh_frame(),
                       "ftp":self.create_ftp_frame(),
                       "nfs":self.create_nfs_frame(),
                       "web":self.create_web_frame()}
        self.create_window()
        if share:
            self.window.set_title("Share Settings")
        else:
            self.window.set_title("Add Share")
        self.window.show_all()
    
    def create_window(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.global_vbox = gtk.VBox(False,5)
        self.window.add(self.global_vbox)
        
        #Service (Drop Down List)
        self.lbl_service = gtk.Label("Service :")
        self.ddl_service = gtk.combo_box_new_text()
        self.ddl_service.connect("changed",self.ddl_service_changed_callback)
        for service in protocols:
            self.ddl_service.append_text(service)
        self.ddl_service.set_active(0)
        self.global_vbox.pack_start(self.build_hbox((self.lbl_service,self.ddl_service)))
        
        #Name
        self.lbl_name = gtk.Label("Name :")
        self.tb_name = gtk.Entry()
        self.global_vbox.pack_start(self.build_hbox((self.lbl_name,self.tb_name)))
        
        #Location (Drop Down List)
        self.lbl_location = gtk.Label("Service :")
        self.ddl_location = gtk.combo_box_new_text()
        for location in locations:
            self.ddl_location.append_text(location)
        self.ddl_location.set_active(0)
        self.global_vbox.pack_start(self.build_hbox((self.lbl_location,self.ddl_location)))
        
        #Mount point
        self.lbl_mount_point = gtk.Label("Mount_Point :")
        self.tb_mount_point = gtk.Entry()
        self.global_vbox.pack_start(self.build_hbox((self.lbl_mount_point,self.tb_mount_point)))
        
        #Frames
        for frame in self.frames:
            self.global_vbox.pack_start(self.frames[frame])

        
        #Server
        self.lbl_server = gtk.Label("Server :")
        self.tb_server = gtk.Entry()
        self.global_vbox.pack_start(self.build_hbox((self.lbl_server,self.tb_server)))
        
        #Automount
        self.cb_automount = gtk.CheckButton("Automount")
        self.global_vbox.pack_end(self.build_hbox((self.cb_automount,)))
    
    def create_smb_frame(self):
        smb_frame = gtk.Frame()
        smb_frame.set_no_show_all(True)
        smb_frame.set_shadow_type(gtk.SHADOW_NONE)
        smb_vbox = gtk.VBox(False,5)
        smb_frame.add(smb_vbox)
        #Share
        self.lbl_share_smb = gtk.Label("Share :")
        self.tb_share_smb = gtk.Entry()
        smb_vbox.pack_start(self.build_hbox((self.lbl_share_smb,self.tb_share_smb)))
        #Login
        self.lbl_login_smb = gtk.Label("Login :")
        self.tb_login_smb = gtk.Entry()
        smb_vbox.pack_start(self.build_hbox((self.lbl_login_smb,self.tb_login_smb)))
        #Password
        self.lbl_password_smb = gtk.Label("Password :")
        self.tb_password_smb = gtk.Entry()
        self.tb_password_smb.set_visibility(False)
        smb_vbox.pack_start(self.build_hbox((self.lbl_password_smb,self.tb_password_smb)))
        smb_vbox.show_all()
        return smb_frame
    
    def create_ssh_frame(self):
        ssh_frame = gtk.Frame()
        ssh_frame.set_no_show_all(True)
        ssh_frame.set_shadow_type(gtk.SHADOW_NONE)
        ssh_vbox = gtk.VBox(False,5)
        ssh_frame.add(ssh_vbox)
        #Path
        self.lbl_path_ssh = gtk.Label("Path :")
        self.tb_path_ssh = gtk.Entry()
        ssh_vbox.pack_start(self.build_hbox((self.lbl_path_ssh,self.tb_path_ssh)))
        #Login
        self.lbl_login_ssh = gtk.Label("Login :")
        self.tb_login_ssh = gtk.Entry()
        ssh_vbox.pack_start(self.build_hbox((self.lbl_login_ssh,self.tb_login_ssh)))
        #Password
        self.lbl_password_ssh = gtk.Label("Password :")
        self.tb_password_ssh = gtk.Entry()
        self.tb_password_ssh.set_visibility(False)
        ssh_vbox.pack_start(self.build_hbox((self.lbl_password_ssh,self.tb_password_ssh)))
        ssh_vbox.show_all()
        return ssh_frame
    
    def create_nfs_frame(self):
        nfs_frame = gtk.Frame()
        nfs_frame.set_no_show_all(True)
        nfs_frame.set_shadow_type(gtk.SHADOW_NONE)
        nfs_vbox = gtk.VBox(False,5)
        nfs_frame.add(nfs_vbox)
        nfs_vbox.show_all()
        return nfs_frame    
    
    def create_ftp_frame(self):
        ftp_frame = gtk.Frame()
        ftp_frame.set_no_show_all(True)
        ftp_frame.set_shadow_type(gtk.SHADOW_NONE)
        ftp_vbox = gtk.VBox(False,5)
        ftp_frame.add(ftp_vbox)
        ftp_vbox.show_all()
        return ftp_frame
    
    def create_web_frame(self):
        web_frame = gtk.Frame()
        web_frame.set_no_show_all(True)
        web_frame.set_shadow_type(gtk.SHADOW_NONE)
        web_vbox = gtk.VBox(False,5)
        web_frame.add(web_vbox)
        web_vbox.show_all()
        return web_frame
    
    #Private methods
    def __get_protocol(self):
        return self.ddl_service.get_model().get_value(self.ddl_service.get_active_iter(),0)
    
    def __get_location(self):
        return self.ddl_location.get_model().get_value(self.ddl_location.get_active_iter(),0)
    
    #Callbacks
    def ddl_service_changed_callback(self,widget=None):
        for frame in self.frames:
            if frame == self.__get_protocol():
                self.frames[frame].show()
            else:
                self.frames[frame].hide()
    def main(self):
        gtk.main()
        
    def delete_event(self,widget,event):
        return False
    
    def destroy(self,widget):
        gtk.main_quit()
    
if __name__ == "__main__":
    test = ShareWindow()
    new_share = test.main()